import Vue from 'vue'
import Vuex from 'vuex'
import {
  test,
  getMerchantsList,
  getEvaluateList,
  getGoodsList,
  getProductList,
  getStatistical,
  getActivity
} from './api.js'
import {
  getParam
} from './api.js'
var Mock = require('mockjs');
var head = '';
var loginHref = 'https://dev.hupovip.cn/hp';  //跳转到登录页面
var copyUrl = 'https://dev.hupovip.cn/hp-wechat-mp';  //商家评论复制url前缀

Vue.use(Vuex);
const state = { //保存的数据
  index: '1', //控制刷新后的菜单选中效果
  shops: [], //售卖渠道
  OTAstation: [],
  Param: [],
  url: head + '/hp',
  userUrl: head + '/hp-wechat-mp',
  serverUrl: head + '/hpserver',
  copyUrl:copyUrl,

  TanProduct: { //产品
    TanVisible: false,
    TanType: 1,
    id: null,
    title: null,
    loading: false,
    pages: 1,
    total: 0,
    scenicSpotList: [],
    hotelList: [],
    operaList: [],
    OTAList: [],
    selfList: []
  },
  TanGoods: { //订单
    TanVisible: false,
    TanType: 1, //1为订单详情，2为异常订单，3为退款，4为房间状态,
    pages: 1,
    total: 0,
    id: null,
    title: null,
    loading: false,
    checkAll: [],
    allGoodsList: [],
    yuyueList: [],
    OTAList: [],
    selfList: [],
    pushDetail: {}
  },
  TanEvaluate: { //评价
    TanVisible: false,
    TanType: 1,
    id: null,
    title: null,
    evaluateList: [],
    pages: 0,
    loading: false,
    detail: {}
  },
  TanMerchants: { //商家
    TanVisible: false,
    TanType: 1, //1为新增商家,3为编辑商家,7为新增剧场,8为编辑剧场
    total: 0,
    pages: 0,
    id: null,
    title: null,
    loading: false,
    groupType: 0,
    SJList: [],
    dramaList: [],
    hotelList: []
  },
  Tanstatistical: {
    TanVisible: false,
    loading:false,
    pages: 0,
    total: 0,
    productList: [],
    waterList: [],
    writeOffList: [],
    backList: []
  },
  activity:{
    loading:false,
    gift:{
      parentTitle:'',
      showType1:true,
      pages:1,
      total:0,
      giveList:[],
      giveDetail:{},
      configList:[],
      configDetail:{},
      historyList:[],
    }
  },
  TanSetting: { //系统设置
    TanVisible: false,
    TanType: 1, //1为退款编辑，2为模板绑定
    id: null,
    title: null
  },
};

const actions = { //执行事件
  wxpush: ({
    commit
  }, json1) => {
    commit('wxpush', json1);
  },
  changeDialog: ({
    commit
  }, json1) => {
    commit('changeDialog', json1);
  },
  closeDialog: ({
    commit
  }, name) => { //name:需要关闭的栏目名称
    commit('closeDialog', name);
  },
  getShops: ({
    commit
  }, data) => { //name:需要关闭的栏目名称
    commit('getShops', data);
  },
  getOTA: ({
    commit
  }, data) => { //name:需要关闭的栏目名称
    commit('getOTA', data);
  },
  //获取产品列表
  getProductScenicSpotList: ({
    commit
  }, json) => {

    var p = getProductList.getScenicSpot(json);
    p.then((res) => {
      var data = res.data.data;
      if (json.type == 1) {
        data.myType = 1;
      } else if (json.type == 2) {
        data.myType = 2;
      } else if (json.type == 3) {
        data.myType = 3;
      } else if (json.type == 4) {
        data.myType = 4;
      } else if (json.type == 5) {
        data.myType = 5;
      }
      commit('getProductScenicSpotList', data)
    })
    p.catch((err) => {
      console.log(err);
    })
  },
  //获取订单列表
  getAllGoodsList: ({
    commit
  }, json) => {
    var p = getGoodsList.getAllGoods(json);
    p.then((res) => {
      if(res.data.code==10001){
        window.location.href=loginHref+"/login"
      }
      var data = res.data;
      if (json.type == 1) {
        data.myType = 1;
      } else if (json.type == 2) {
        data.myType = 2;
      } else if (json.type == 3) {
        data.myType = 3;
      } else if (json.type == 4) {
        data.myType = 4;
      } else {
        data.myType = '';
      }
      commit('getAllGoodsList', data)
    })
    p.catch((err) => {
      console.log(err);
    })
  },
  //评论列表获取
  getEvaluateList: ({
    commit
  }, json) => {
    commit('showLoading', 'TanEvaluate');
    var p = getEvaluateList.getEvaluate(json);
    p.then((res) => {
      if(res.data.code==10001){
        window.location.href=loginHref+"/login"
      }
      var data = res.data.data;
      commit('getEvaluateList', data)
      commit('closeLoading', 'TanEvaluate');
    })
    p.catch((err) => {
      console.log(err);
    })
  },
  //商家列表获取
  getMerchantsSJ: ({
    commit
  }, json) => {
    var p = getMerchantsList.getSJ(json);
    p.then((res) => {
      if(res.data.code==10001){
        window.location.href=loginHref+"/login";
        return;
      }
      commit('getMerchantsSJ', res.data.data)
    });
    p.catch((err) => {
      console.log(err);
    });
  },
  getMerchantsHotel: ({
    commit
  }, json) => {
    var p = getMerchantsList.getHotel(json);
    p.then((res) => {
      if(res.data.code==10001){
        window.location.href=loginHref+"/login";
        return;
      }
      commit('getMerchantsHotel', res.data.data)
    });
    p.catch((err) => {
      console.log(err);
    });
  },
  getMerchantsDrama: ({
    commit
  }, json) => {
    var p = getMerchantsList.getDrama(json);
    p.then((res) => {
      if(res.data.code==10001){
        window.location.href=loginHref+"/login";
        return;
      }
      commit('getMerchantsDrama', res.data.data)
    })
    p.catch((err) => {
      console.log(err);
    })
  },
  //营销
  activityCloseType: ({
    commit
  }, json1) => {
    commit('activityCloseType', json1);
  },
  activityShowType: ({
    commit
  }, json1) => {
    commit('activityShowType', json1);
  },
  activityChangeTitle: ({
    commit
  }, json1) => {
    commit('activityChangeTitle', json1);
  },
  getActivityGiftConfigList: ({
    commit
  }, json1) => {
    commit('showLoading', 'activity');
    var p = getActivity.getGiftConfig(json1);
    p.then((res)=>{
      commit('getActivityGiftConfigList', res.data.data);
      commit('closeLoading', 'activity');
    })
  },
  getActivityGiftGiveList: ({
    commit
  }, json1) => {
    commit('showLoading', 'activity');
    var p = getActivity.getGiftGive(json1);
    p.then((res)=>{
      commit('getActivityGiftGiveList', res.data.data);
      commit('closeLoading', 'activity');
    })
  },
  getActivityGiftHistoryList: ({
    commit
  }, json1) => {
    commit('showLoading', 'activity');
    var p = getActivity.getGiftHistory(json1);
    p.then((res)=>{
      commit('getActivityGiftHistoryList', res.data.data);
      commit('closeLoading', 'activity');
    })
  },
  activityChangeConfigDetail: ({
    commit
  }, json1) => {
      commit('activityChangeConfigDetail', json1);
  },
  activityChangeGiveDetail: ({
    commit
  }, json1) => {
      commit('activityChangeGiveDetail', json1);
  },

  //数据分析列表
  getStatisticalProduct: ({ commit }, json) => {
    var p = getStatistical.getProduct(json);
    p.then((res) => {
      if(res.data.code==10001){
        window.location.href=loginHref+"/login";
        return;
      }
      commit('getStatisticalProduct', res.data.data)
    })
    p.catch((err) => {
      console.log(err);
    })
  },
  //获取关联列表
  getByParam: ({
    commit
  }, json) => {
    var p = getParam(json);
    p.then((res) => {
      if(res.data.code==10001){
        window.location.href=loginHref+"/login";
        return;
      }
      commit('getByParam', res.data)
    });
    p.catch((err) => {

    })
  },
  //loading控制
  showLoading: function({
    commit
  }, name) {
    commit('showLoading', name);
  },
  closeLoading: function({
    commit
  }, name) {
    commit('closeLoading', name);
  }
};

const mutations = { //改变数据的事件
  wxpush: (state, json1) => {
    console.log(json1 + "zzz");
    state.TanGoods.pushDetail = json1;
  },
  changeDialog: (state, json1) => {
    state[json1.name].TanType = json1.type;
    state[json1.name].TanVisible = true;
    if (json1.id) {
      state[json1.name].id = json1.id;
    }
    if (json1.groupType)(
      state[json1.name].groupType = json1.groupType
    )
    state[json1.name].title = json1.title;
    if (json1.detail) {
      state[json1.name].detail = json1.detail
    }
    if (json1.pushId) {
      state[json1.name].pushId = json1.pushId
    }
  },
  closeDialog: (state, name) => {
    state[name].TanVisible = false;
  },
  getShops: (state, data) => {
    state.shops = data;
  },
  getOTA: (state, data) => {
    state.OTAstation = data;
  },
  //产品栏目
  getProductScenicSpotList: (state, json) => {
    if (json.myType == 1) {
      state.TanProduct.scenicSpotList = json.result;
    } else if (json.myType == 2) {
      state.TanProduct.hotelList = json.result;
    } else if (json.myType == 3) {
      state.TanProduct.operaList = json.result;
    } else if (json.myType == 4) {
      state.TanProduct.OTAList = json.result;
    } else if (json.myType == 5) {
      state.TanProduct.selfList = json.result;
    }
    state.TanProduct.pages = json.pages;
    state.TanProduct.total = json.total;
    state.TanProduct.loading = false;
  },
  //订单栏目
  getAllGoodsList: (state, json) => {
    if (json.myType == 1) {
      state.TanGoods.yuyueList = json.data.result;
      for (let i in json.data.result) {
        state.TanGoods.checkAll.push(json.data.result[i].id)
      }
    } else if (json.myType == 2) {
      state.TanGoods.OTAList = json.data.result;
    } else if (json.myType == 3) {
      state.TanGoods.backList = json.data.result;
    } else if (json.myType == 4) {
      state.TanGoods.selfList = json.data.result;
    } else {
      state.TanGoods.allGoodsList = json.data.result;
    }
    state.TanGoods.pages = json.data.pages;
    state.TanGoods.total = json.data.total;
    state.TanGoods.loading = false;
  },
  //评论栏目
  getEvaluateList: (state, json) => {
    state.TanEvaluate.evaluateList = json;
    state.TanEvaluate.pages = json.pages;
  },
  //商家栏目
  getMerchantsSJ: (state, data) => {
    state.TanMerchants.SJList = data.result;
    state.TanMerchants.loading = false;
    state.TanMerchants.pages = data.pages;
    state.TanMerchants.total = data.total;
  },
  getMerchantsHotel: (state, data) => {
    state.TanMerchants.hotelList = data.result;
    state.TanMerchants.loading = false;
    state.TanMerchants.pages = data.pages;
    state.TanMerchants.total = data.total;
  },
  getMerchantsDrama: (state, data) => {
    state.TanMerchants.dramaList = data.result;
    state.TanMerchants.loading = false;
    state.TanMerchants.pages = data.pages;
    state.TanMerchants.total = data.total;
  },
  //营销
  activityChangeTitle:(state, json1) => {
    state.activity[json1.parentName][json1.titleName] = json1.title;
  },
  activityCloseType:(state, json1) => {
    state.activity[json1.parentName][json1.typeName] = false;
  },
  activityShowType:(state, json1) => {
    state.activity[json1.parentName][json1.typeName] = true;
  },
  getActivityGiftConfigList:(state, json1) => {
    state.activity.gift.configList = json1.result;
    state.activity.gift.pages = json1.pages;
    state.activity.gift.total = json1.total;
  },
  getActivityGiftGiveList:(state, json1) => {
    state.activity.gift.giveList = json1.result;
    state.activity.gift.pages = json1.pages;
    state.activity.gift.total = json1.total;
  },
  getActivityGiftHistoryList:(state, json1) => {
    state.activity.gift.historyList = json1.result;
    state.activity.gift.pages = json1.pages;
    state.activity.gift.total = json1.total;
  },
  activityChangeConfigDetail:(state, json1) => {
    state.activity.gift.configDetail = json1;
  },
  activityChangeGiveDetail:(state, json1) => {
    state.activity.gift.giveDetail = json1;
  },
  //数据分析
  getStatisticalProduct: (state, data) => {
    state.Tanstatistical.productList = data.result;
    state.Tanstatistical.loading = false;
    state.Tanstatistical.pages = data.pages;
    state.Tanstatistical.total = data.total;
  },
  getByParam: (state, data) => {
    state.Param = data.data;
  },
  //loading变化
  showLoading: function(state, name) {
    state[name].loading = true;
  },
  closeLoading: function(state, name) {
    state[name].loading = false;
  }
}

const getters = {
  evaluateListId: state => {
    return state.TanEvaluate.evaluateList.result.map(function(e) {
      return e.id;
    });
  }
}

export const store = new Vuex.Store({ //创建一个Vuex
  state,
  mutations,
  actions,
  getters
});

export function zhuan(json) {
  var txt = json;
  var c = eval("(" + txt + ")");
  txt = Mock.mock(c);
  return txt;
}
